package com.proxy.autoclick.checker.controller;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

import com.proxy.autoclick.checker.dao.ProxyDao;
import com.proxy.autoclick.checker.thread.ThreadPool;
import com.proxy.autoclick.common.util.DataSourceUtil;
import com.proxy.autoclick.common.valueobject.Proxy;
import com.proxy.autoclick.common.valueobject.ValidProxyQueue;
import com.proxy.autoclick.ui.layout.MainFrame;

/**
 * Proxy checking follow control.
 *
 */
@Named
public class ProxyCheckingController {
	
	private ProxyDao proxyDao = new ProxyDao();
	
	@Inject
	private ThreadPool threadPool;
	
	@Inject
	private MainFrame mainframe;
	
	@Inject
	private ValidProxyQueue validProxyQueue;
	
	private PoolingClientConnectionManager connectionManager;
	private HttpClient httpclient;
	
	public ProxyCheckingController()
	{
		// Create an HttpClient with the ThreadSafeClientConnManager.
		// This connection manager must be used if more than one thread will
		// be using the HttpClient.
		connectionManager = new PoolingClientConnectionManager();
		connectionManager.setMaxTotal(50);
		connectionManager.setDefaultMaxPerRoute(5);
		
		httpclient = new DefaultHttpClient(connectionManager);
		
	}
	
	/**
	 * Get proxy data from proxyDao object, start threadPool object.
	 * @throws Exception 
	 */
	public boolean startCheck() throws Exception
	{
		mainframe.setTextAreaText("Checking proxies if valid.");
		this.proxiesChecking();
		mainframe.setTextAreaText("finished checking proxy.");
		
		return Boolean.TRUE;
	}
	
	private void proxiesChecking() throws Exception
	{
//		this.putValidProxyToQueue();
		
		mainframe.setTextAreaText("Delete valid proxies pre run.");
		
		this.deleteValidProxyAfterStart();
		
		this.checkProxyFromTable();
		
	}
	
	private void deleteValidProxyAfterStart() throws SQLException
	{
		proxyDao.deleteValidProxyAfterStart();
	}
	
	private void putValidProxyToQueue() throws SQLException
	{
		List<Proxy> myProxyList = proxyDao.getValidProxyList();
		if(myProxyList != null && !myProxyList.isEmpty())
		{
			mainframe.setProxyCounterText(myProxyList.size());
		}
		validProxyQueue.put(myProxyList);
	}
	
	private void checkProxyFromTable() throws Exception
	{
		int startIndex = 0, size = 100;
		List<Proxy> myProxyList = null;
		while(true)
		{
			mainframe.setTextAreaText(String.format("Checking next %s proxies.", size));
			myProxyList = proxyDao.getProxyList(startIndex, size);
			/*if(startIndex == 0 && myProxyList.isEmpty())
			{
				mainframe.setTextAreaText("Empty proxy table.");
				break;
			}*/
			if(myProxyList == null || myProxyList.isEmpty())
				break;
			
			threadPool.start(httpclient,myProxyList);
		}
	}
	
	@PreDestroy
	public void stopCheck() throws SQLException
	{
		httpclient.getConnectionManager().shutdown();
		DataSourceUtil.closeDataSource();
	}

}
